<template>
  <el-dialog :title="formData.id?'编辑经纪':'新增经纪'" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :rules="rules" :model="formData" label-width="80px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入电话号码" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="formData.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" type="textarea" placeholder="请输入描述" autosize="true" />
      </el-form-item>
      <el-form-item label="封面">
        <ImageUpload ref="imgupload" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="confirmFn">确认</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </span>
  </el-dialog>
</template>

<script>
import { addAgents, updateAgent } from '@/api/agent'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    curData: {
      type: Object,
      required: true
    },
    getAgents: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '电话不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '电话号码格式不正确', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    edit() {
      this.formData = this.curData
      this.$refs.imgupload.fileList = []
      this.$refs.imgupload.fileList.push({
        url: this.curData.image
      })
    },
    // 确认 / 新增 / 编辑
    async confirmFn() {
      const upload = this.$refs.imgupload
      this.$refs.form.validate()
      this.formData.image = upload.fileList[0] && upload.fileList[0].url
      if (this.formData.id) {
        await updateAgent(this.formData)
      } else {
        // 新增
        await addAgents(this.formData)
      }
      // 关闭弹窗
      this.closeFn()
      // 更新列表
      this.getAgents()
    },
    // 关闭弹窗
    closeFn() {
      // 清空表单
      this.formData = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      // 清空校验
      this.$refs.form.resetFields()
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      // 清空图片
      this.$refs.imgupload.fileList = []
    }
  }
}
</script>

<style>

</style>
